#include "errorcodedescriptionview.h"
#include "sconst.h"

ErrorCodeDescriptionView::ErrorCodeDescriptionView(QWidget *parent, bool breadonly) : QWidget(parent)
{
    createView(breadonly);
}

void ErrorCodeDescriptionView::createView(bool breadonly)
{
  QSizePolicy    sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  QSizePolicy    errorCodeIDLabelsizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
  QSizePolicy    shortdescriptioneditsizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
  QPalette       palette_l;

  verticalLayout             = new QVBoxLayout(this);
  hl_header_description      = new QHBoxLayout();
  hs_module_modulename       = new QSpacerItem(5, 20, QSizePolicy::Minimum, QSizePolicy::Minimum);
  hs_modulename_errorcode    = new QSpacerItem(40, 20, QSizePolicy::Minimum, QSizePolicy::Minimum);
  hs_errorcodename_2_id      = new QSpacerItem(5, 20, QSizePolicy::Minimum, QSizePolicy::Minimum);
  ModuleName_Label           = new QLabel(this);
  ErrorCodeLabel             = new QLabel(this);
  Module_Label               = new QLabel(this);
  ErrorCodeIDLabel           = new QLabel(this);
  header_shortdescription    = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
  ShortDescriptionLabel      = new QLabel(this);
  vs_module_shortdescription = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
  ShortDescriptionEdit       = new QTextEdit(this);
  hs_ShortDescFullDescr      = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
  FullDescriptionLabel       = new QLabel(this);
  vs_shortdescr_fulldescr    = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
  FullDescriptionEdit        = new QTextEdit(this);
  doneButton                 = new QPushButton(this);


  if(breadonly == true) {
    doneButton->setEnabled(false);

  } else
  {
    doneButton->setEnabled(true);
    doneButton->setAutoFillBackground(true);
    doneButton->setFlat(true);
    palette_l = doneButton->palette();
    palette_l.setColor(QPalette::Button,QColor(82,110,166));
    doneButton->setPalette(palette_l);
    doneButton->setIcon(QIcon(SC_IMG_CHECK));
    doneButton->setIconSize(QSize(16,16));
    connect(doneButton, SIGNAL(clicked()), this, SLOT(doneButtonClicked()));
  }

  FullDescriptionEdit->setReadOnly(breadonly);
  ShortDescriptionEdit->setReadOnly(breadonly);

  Module_Label->setText(SC_MODULE);
  ErrorCodeLabel->setText(SC_ERROR_CODE);
  ShortDescriptionLabel->setText(SC_SHORT_DESCRIPTION);
  FullDescriptionLabel ->setText(SC_FULL_DESCRIPTION);

  verticalLayout->setSizeConstraint(QLayout::SetMinimumSize);
  verticalLayout->setContentsMargins(0, 0, 0, 0);

  FullDescriptionEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

  hl_header_description->addWidget(Module_Label);
  hl_header_description->addItem(hs_module_modulename);

  sizePolicy.setHorizontalStretch(0);
  sizePolicy.setVerticalStretch(0);
  sizePolicy.setHeightForWidth(ModuleName_Label->sizePolicy().hasHeightForWidth());
  ModuleName_Label->setSizePolicy(sizePolicy);

  hl_header_description->addWidget(ModuleName_Label);
  hl_header_description->addItem(hs_modulename_errorcode);

  sizePolicy.setHorizontalStretch(0);
  sizePolicy.setVerticalStretch(0);
  sizePolicy.setHeightForWidth(ErrorCodeLabel->sizePolicy().hasHeightForWidth());
  ErrorCodeLabel->setSizePolicy(sizePolicy);

  hl_header_description->addWidget(ErrorCodeLabel);
  hl_header_description->addItem(hs_errorcodename_2_id);

  errorCodeIDLabelsizePolicy.setHorizontalStretch(0);
  errorCodeIDLabelsizePolicy.setVerticalStretch(0);
  errorCodeIDLabelsizePolicy.setHeightForWidth(ErrorCodeIDLabel->sizePolicy().hasHeightForWidth());
  ErrorCodeIDLabel->setSizePolicy(errorCodeIDLabelsizePolicy);

  hl_header_description->addWidget(ErrorCodeIDLabel);
  hl_header_description->addWidget(doneButton);

  verticalLayout->addLayout(hl_header_description);
  verticalLayout->addItem(header_shortdescription);

  verticalLayout->addWidget(ShortDescriptionLabel);

  verticalLayout->addItem(vs_module_shortdescription);

  shortdescriptioneditsizePolicy.setHorizontalStretch(0);
  shortdescriptioneditsizePolicy.setVerticalStretch(0);
  shortdescriptioneditsizePolicy.setHeightForWidth(ShortDescriptionEdit->sizePolicy().hasHeightForWidth());
  ShortDescriptionEdit->setSizePolicy(shortdescriptioneditsizePolicy);
  ShortDescriptionEdit->setMinimumSize(QSize(0, 20));
  ShortDescriptionEdit->setBaseSize(QSize(0, 20));
  ShortDescriptionEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

  verticalLayout->addWidget(ShortDescriptionEdit);
  verticalLayout->addItem(hs_ShortDescFullDescr);
  verticalLayout->addWidget(FullDescriptionLabel);
  verticalLayout->addItem(vs_shortdescr_fulldescr);
  verticalLayout->addWidget(FullDescriptionEdit);

}

void ErrorCodeDescriptionView::setErrorCode(Errorcode ec)
{
  ModuleName_Label->setText(ec.getModule().getModuleName().c_str());
  ErrorCodeIDLabel->setText(QString::number(ec.getErrorID()));
  FullDescriptionEdit->setText(ec.getLongDescription().c_str());
  ShortDescriptionEdit->setText(ec.getShortDescription().c_str());
}


void ErrorCodeDescriptionView::doneButtonClicked(void)
{
}
